package com.hongyuan.sellcoach.ui.home.activity;

import android.os.Bundle;
import androidx.lifecycle.ViewModelProviders;
import com.google.gson.reflect.TypeToken;
import com.hongyuan.mvvmhabitx.utils.ToastUtils;
import com.hongyuan.sellcoach.R;
import com.hongyuan.sellcoach.base.AppViewModelFactory;
import com.hongyuan.sellcoach.base.CustomActivity;
import com.hongyuan.sellcoach.data.Constants;
import com.hongyuan.sellcoach.data.md5.BaseUtil;
import com.hongyuan.sellcoach.databinding.ActivityAppointmentStudentsBinding;
import com.hongyuan.sellcoach.ui.home.beans.StudentsBeans;
import com.hongyuan.sellcoach.ui.home.view_mode.AppointmentStudentsViewMode;
import com.hongyuan.sellcoach.utils.CustomDialog;
import com.hongyuan.sellcoach.utils.GsonUtil;
import java.util.ArrayList;
import java.util.List;

import me.tatarka.bindingcollectionadapter2.BR;

public class AppointmentStudentsActivity extends CustomActivity<ActivityAppointmentStudentsBinding, AppointmentStudentsViewMode> {

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_appointment_students;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public AppointmentStudentsViewMode initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(AppointmentStudentsViewMode.class);
    }


    @Override
    public void initView() {
        super.initView();

        setOnRefresh(binding.refresh,REFRESH_0X4);
        binding.comBack.setOnClickListener(view -> finish());

    }

    @Override
    public void initData() {
        super.initData();

        viewModel.courseName.set("请选择");
        viewModel.coachName.set("请选择");

        //初始化时间选择器的viewModel
        binding.selectTime.setViewModel(viewModel);

        //获取课程数据
        viewModel.clearParams().requestData(Constants.CP_LIST);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.showCps.observe(this, list -> {
            CustomDialog.scroller(this, list, "选择课程", (v, message,id) -> {
                binding.courseName.setTag(id);
                viewModel.courseName.set(message);
                viewModel.setParams("cp_id",id).requestData(Constants.PRIVITE_COACH_LIST);

                //清除教练选择项以及时间选择项
                viewModel.coachName.set("请选择");
                //清除所选学员
                viewModel.observableList.clear();
                //清除时间选择
                binding.selectTime.clearData();
            });
        });

        viewModel.uc.showCoachs.observe(this, list -> {
            CustomDialog.scroller(this, list, "选择教练", (v, message,id) -> {
                binding.coachName.setTag(id);
                viewModel.coachName.set(message);
                //viewModel.setParams("cp_id",viewModel.cpUtils.getUseId(message)).requestData(Constants.PRIVITE_COACH_LIST);
                viewModel.requestData(Constants.GET_PLAN_DATA);
            });
        });

        viewModel.uc.setTimeTopData.observe(this, topTimeBeans -> {
            //教练已从选先清除原始数据
            binding.selectTime.clearData();
            //清除所选学员
            viewModel.observableList.clear();

            binding.selectTime.setTopData(String.valueOf(binding.coachName.getTag()),topTimeBeans);
        });

        viewModel.uc.setTimeContentData.observe(this, contentTimeBeans -> {
            binding.selectTime.setContentData(contentTimeBeans);
        });

        viewModel.uc.selectStudents.observe(this, aVoid -> {
            if(!BaseUtil.isValue(binding.courseName.getTag())){
                ToastUtils.showShort("请选择课程");
                return;
            }
            if(!BaseUtil.isValue(binding.selectTime.getStartTime())){
                ToastUtils.showShort("请选择上课时间");
                return;
            }
            Bundle bundle = new Bundle();
            bundle.putString("cp_id",String.valueOf(binding.courseName.getTag()));
            bundle.putString("start_date",binding.selectTime.getStartTime());
            startForResult(StudentsActivity.class,bundle);
        });

        viewModel.uc.submit.observe(this,aVoid -> {
            if(!BaseUtil.isValue(binding.courseName.getTag())){
                ToastUtils.showShort("请选择课程");
                return;
            }
            if(!BaseUtil.isValue(binding.selectTime.getStartTime())){
                ToastUtils.showShort("请选择上课时间");
                return;
            }
            if(!BaseUtil.isValue(binding.coachName.getTag())){
                ToastUtils.showShort("请选择上课教练");
                return;
            }
            String ids = viewModel.getCourseIdS();
            if(!BaseUtil.isValue(ids)){
                ToastUtils.showShort("请选择学员");
                return;
            }

            viewModel.clearParams().setParams("jl_mid",String.valueOf(binding.coachName.getTag()))
                    .setParams("cp_id",String .valueOf(binding.courseName.getTag()))
                    .setParams("start_date",binding.selectTime.getStartTime())
                    .setParams("my_course_id",ids).requestData(Constants.ADD_COURSE_PRIVITE_APPOINTMENT);
        });
    }

    @Override
    protected void forResult(Bundle bundle) {
        super.forResult(bundle);

        String slectStr = bundle.getString("slectData");

        if(!BaseUtil.isValue(slectStr)){
            viewModel.observableList.clear();
            viewModel.stuList.set(null);
        }else{

            List<StudentsBeans.ListBean> selectList = GsonUtil.getGson().fromJson(slectStr, new TypeToken<List<StudentsBeans.ListBean>>(){}.getType());
            if(viewModel.stuList.get() == null){
                viewModel.stuList.set(new ArrayList<>());
            }
            viewModel.findOrAdd(selectList);
        }
    }
}
